module common.weathereffect;

import share.types;

import allegro5.allegro;
import allegro5.allegro_primitives;
import allegro5.transformations;
import allegro5.bitmap_io;


const _MIST_STATE_START = 0;

public class Mist
{
    private ALLEGRO_BITMAP*     _mist_bmp;
    private bool                m_bActive;
    private ubyte               m_bMistState;   

    private ALLEGRO_VERTEX      m_avBillboard[4];
    private FloatPoint          m_vTrans;
    
    
    public this()
    {
        initMist();
    }
    
    // ¾È°³ »ý¼º.
    public void initMist()
    {
        _mist_bmp = null;
        m_bActive = false;
        m_bMistState = _MIST_STATE_START;

        m_avBillboard = m_avBillboard.init;
        m_vTrans = FloatPoint.zero;
    }

    // ÅØ½ºÃçµîÀÇ ¸Þ¸ð¸®ÇØÁ¦.
    public void destroyMist()
    {
        if (_mist_bmp !is null)
        {
            al_destroy_bitmap(_mist_bmp);
            _mist_bmp = null;
        }

        initMist();
    }

    public void setupMist()
    {
        _mist_bmp = al_load_bitmap("mist.bmp");

        /+
        if ( _mist_bmp !is null )
        {
            immutable color = al_map_rgba(100, 200, 200, 1);
            int bmp_width = al_get_bitmap_width(_mist_bmp);
            int bmp_height = al_get_bitmap_height(_mist_bmp);

            m_avBillboard[0] = ALLEGRO_VERTEX(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, color);
            m_avBillboard[1] = ALLEGRO_VERTEX(800.0f,0.0f, 0.0f, bmp_width, 0.0f, color);
            m_avBillboard[2] = ALLEGRO_VERTEX( 0.0f, 600.0f, 0.0f, 0.0f, bmp_height, color);
            m_avBillboard[3] = ALLEGRO_VERTEX( 800.0f,600.0f, 0.0f, bmp_width, bmp_height, color);
        }
        +/
    }
    

    
    // ¾È°³ ÁøÇà.(¾È°³½ÃÀÛ½ÃÁ¡, ¾È°³ÁøÇà, ¾È°³Á¾·á½ÃÁ¡¿¡¼­ ¼­·Î ´Ù¸£´Ù.)
    public void progressMist()
    {
        static int nCnt = 0;
        
        nCnt++;
        
        if ( nCnt <= 800 )
        {
            m_vTrans.setX( m_vTrans.getX() - 1.9f ); 
            m_vTrans.setY( m_vTrans.getY() - 0.2f );
        }
        else if ( nCnt >= 800 && nCnt <= 1600 )
        {
            m_vTrans.setX( m_vTrans.getX() + 1.9f ); 
            m_vTrans.setY( m_vTrans.getY() + 0.2f );
        }
        else
        {
            nCnt = 0;
        }
    }
    
    public void renderMist()
    {
        //immutable tint_color = al_map_rgba(100, 200, 200, 1);    // Blue
        immutable tint_color = al_map_rgba(200, 100, 200, 1);    // Purple
        
        al_draw_tinted_scaled_rotated_bitmap(_mist_bmp,
                                             tint_color, 0f, 0f, m_vTrans.getX(), m_vTrans.getY(), 5.0f, 2.0f,
                                             0f, 0);

        /*ALLEGRO_TRANSFORM identity, trans, scale, world;
        
        al_identity_transform(&identity);
        al_identity_transform(&world);
        //vTrans.x = (FLOAT)m_shPosX-400;
        //vTrans.y = (FLOAT)-m_shPosY+300;
        al_build_transform(&world, m_vTrans.getX(), -m_vTrans.getY(), 5.0f, 2.0f, 0);
        //al_build_transform(&world, m_vTrans.getX()-400, -m_vTrans.getY()+300, 3.0f, 1.5f, 0);

        al_use_transform(&world);
        al_draw_prim(&m_avBillboard, null, _mist_bmp, 0, 4, ALLEGRO_PRIM_TYPE.ALLEGRO_PRIM_TRIANGLE_STRIP);
        //al_draw_prim(&m_avBillboard, null, _mist_bmp, 0, 4, ALLEGRO_PRIM_TYPE.ALLEGRO_PRIM_TRIANGLE_FAN);
        al_use_transform(&identity);*/
    }
}

